package com.lwj.liwanjia.view.activity.like;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.LikeMoreAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.AddGwcBean;
import com.lwj.liwanjia.bean.CollectBean;
import com.lwj.liwanjia.bean.MoreLikeBean;
import com.lwj.liwanjia.impl.AddGwcView;
import com.lwj.liwanjia.impl.CollectView;
import com.lwj.liwanjia.impl.MoreLikeView;
import com.lwj.liwanjia.presenter.AddGwcPresenter;
import com.lwj.liwanjia.presenter.CollectPresenter;
import com.lwj.liwanjia.presenter.MoreLikePresenter;
import com.lwj.liwanjia.utils.NetWorkUtil;
import com.lwj.liwanjia.utils.PopUtils;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LikeMoreActivity extends BaseActivity implements MoreLikeView, CollectView, SuperRecyclerView.LoadingListener {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.like_rlv)
    SuperRecyclerView likeRlv;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    private LikeMoreAdapter adapter;
    private List<MoreLikeBean.DataBean.CommendityBean> list = new ArrayList<>();
    private MoreLikePresenter moreLikePresenter;
    private int page = 0;
    private int allPage;
    private String token;
    private CollectPresenter collectPresenter;
    private int clickPosition = 0;//点击的item项位置

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("猜你喜欢");
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(R.mipmap.fenlei);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        likeRlv.setLayoutManager(manager);
        likeRlv.setRefreshEnabled(true);
        likeRlv.setLoadMoreEnabled(true);
        likeRlv.setLoadingListener(this);
        adapter = new LikeMoreAdapter(this, list);
        likeRlv.setAdapter(adapter);
        token = SpfUtils.getSpfUtils(this).getToken();
        moreLikePresenter = new MoreLikePresenter(this);
        collectPresenter = new CollectPresenter(this);
        listener();
    }

    private void listener() {
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(LikeMoreActivity.this, ShangpinDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", list.get(position).getCommodityCode());
                bundle.putString("commodityType", list.get(position).getCommodityType());
                intent.putExtras(bundle);
                startActivity(intent);
                clickPosition = position;
            }
        });
        adapter.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.image_like:
                        clickPosition = position;
                        collectPresenter.getCollect(token, list.get(position).getCommodityType(), list.get(position).getCommodityCode());
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        moreLikePresenter.getMoreLike(token,page);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_like_more;
    }


    @OnClick({R.id.tv_left, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.iv_right:
                PopUtils.getInstance().showPopupWindow(LikeMoreActivity.this, ivRight);
                break;
        }
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败！", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void getCollect(CollectBean collectBean) {
        if (collectBean.getState() == 0) {
            Toast.makeText(this, "收藏失败！", Toast.LENGTH_SHORT).show();
        } else if (collectBean.getState() == 1) {
            if (collectBean.isData()) {
                list.get(clickPosition).setCollect(1);
            } else {
                list.get(clickPosition).setCollect(0);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getMoreLike(MoreLikeBean moreLikeBean) {
        if (moreLikeBean.getState() == 1) {
            allPage = moreLikeBean.getData().get(0).getAllPage();
            if (page == 0) {
                list.clear();
            }
            list.addAll(moreLikeBean.getData().get(0).getCommendity());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh() {
        page = 0;
        initData();
        likeRlv.completeRefresh();
    }

    @Override
    public void onLoadMore() {
        if (page >= allPage - 1) {
            likeRlv.setNoMore(true);
            return;
        }
        page++;
        initData();
        likeRlv.completeLoadMore();
    }

    @Override
    protected void onResume() {
        super.onResume();
        page = 0;
        initData();
        likeRlv.scrollToPosition(clickPosition);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (moreLikePresenter != null) {
            moreLikePresenter = null;
        }
        if (collectPresenter != null) {
            collectPresenter = null;
        }
    }
}
